﻿using System;
using System.Collections.Generic;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;

public partial class IFRS_WIN18_2E : PageBase
{
    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewMenu();
            BindControl();
            BindData();
            rbtIsAllColleteral_SelectedIndexChanged(null, null);
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCONFIRM_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN18_1S.aspx");
    }

    protected void rbtIsAllColleteral_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rbtIsAllColleteral.SelectedValue == "1") // ทั้งหมด
        {
            ddlColleteralTypeID.Enabled = false;
            spanColleteralTypeID.Visible = false;
            reqColleteralTypeID.Enabled = false;
        }
        else // หลักประกัน 
        {
            ddlColleteralTypeID.Enabled = true;
            spanColleteralTypeID.Visible = true;
            reqColleteralTypeID.Enabled = true;
        }
    }

    #endregion

    #region === Method ===

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN18_2E;
    }
    private void BindControl()
    {
        btnSAVE.ValidationGroup = "Master";

        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqCostOfFund.SetRequiredField(lblCostOfFund, txtCostOfFund, btnSAVE);
        reqCostOfFund.InitialValue = "0.00";
        reqColleteralTypeID.SetRequiredField(lblColleteralTypeID, ddlColleteralTypeID, btnSAVE);
        reqColleteralTypeID.CssClass = "required_inline";

        ddlColleteralTypeID.SetData(DDL_TYPE.CollateralType, string.Empty, false, true);
    }
    private void BindData()
    {
        if (Convert.ToString(Request.QueryString["id"]) != string.Empty)
        {

            var id = Convert.ToInt32(Request.QueryString["id"]);
            var dt = new SPR_COSTOFFUND().SEARCH_BYID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                var list = new List<CostOfFund>();
                ConvertHelper.ConvertDataTableToObject(dt, list);

                txtTransactionDate.Text = list[0].TransactionDate.DateToString();

                rbtJudgementStatement.SelectedValue = list[0].JudgementStatement.IntToString();

                //rbtIsAllColleteral.SelectedValue = list[0].IsAllColleteral ? "1" : "0";
                if (list[0].IsAllColleteral != null)
                    switch (list[0].IsAllColleteral)
                    {
                        case true:
                            rbtIsAllColleteral.SelectedValue = "1";
                            break;
                        case false:
                            rbtIsAllColleteral.SelectedValue = "0";
                            break;
                    }


                if (list[0].ColleteralTypeID != null)
                    ddlColleteralTypeID.SelectedValue = list[0].ColleteralTypeID.IntToString();

                txtCostOfFund.Text = list[0].CostOfFundValue.DecimalToString();
            }
        }
    }
    private void SaveData()
    {
        if (!Page.IsValid) return;

        var dal = new SPR_COSTOFFUND();
        var models = new CostOfFund();

        models.CostOfFundID = Convert.ToInt32(Request.QueryString["id"]);

        models.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
        models.JudgementStatement = rbtJudgementStatement.SelectedValue.AsInt32();
        if (rbtIsAllColleteral.SelectedValue == "1") // ทั้งหมด
        {
            models.IsAllColleteral = true;
            models.ColleteralTypeID = null;
        }
        else // หลักประกัน
        {
            models.IsAllColleteral = false;
            models.ColleteralTypeID = ddlColleteralTypeID.SelectedValue.AsInt32();
        }
        models.CostOfFundValue = txtCostOfFund.Text.AsDecimal();

        models.SetStandardField();

        var result = dal.UPDATE(models);
        if (result > 0)
            JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN18_2E.aspx?&id={0}", models.CostOfFundID)));                
        else // Duplicate
            Utility.AlertMessage(Page,result == -1 ? Resources.ifrsResource.Duplicate : Resources.ifrsResource.CanNotSaveData);
    }

    #endregion
}